﻿var visible = [], setLayerVisibility;
var map;
function getMapUrl() {
    var mapurl;
    $.ajax({
        url: "config/mapurl.xml",
        dataType: 'xml',
        type: 'GET',
        async:false,
        timeout: 2000,
        error: function (xml) {
            alert("加载XML 文件出错！");
        },
        success: function (xml) {
            $(xml).find("Map").each(function (i) {
                mapurl = $(this).children("url").text();
            });
           
        }
    });
    return mapurl;
}
require(["dojo/parser", "dojo/on", "esri/map", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/layers/ArcGISTiledMapServiceLayer",
  "tdlib/TDTLayer", "tdlib/TDAnnoLayer", "dojo/domReady!"],
function (parser, on, Map, ArcGISDynamicMapServiceLayer, ArcGISTiledMapServiceLayer,TDTLayer, TDTAnnoLayer) {
        parser.parse();
        map = new Map("mapDiv", {
            logo: false,
            center: [121.48, 31.22],
            zoom: 8,
            sliderPosition: "top-left",
            sliderOrientation:"horizontal"
        });
       
        map.infoWindow.resize(400, 400);
        var basemap = new TDTLayer();
        map.addLayer(basemap);
        var annolayer = new TDTAnnoLayer();
        map.addLayer(annolayer);
        var mapUrl = "http://192.168.1.101:6080/arcgis/rest/services/map09/MapServer";
        var dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer(mapUrl);
        dynamicMapServiceLayer.opacity = 0.8;
        map.addLayer(dynamicMapServiceLayer); 
        dojo.connect(dynamicMapServiceLayer, "onLoad", loadLayerList);    
        function loadLayerList(layers) {
            
            var html = "", content = "";
            var infos = layers.layerInfos;
            var option1, option2, option3, option4;
            for (var i = 0, length = infos.length; i < length; i++) {
                var info = infos[i];

                visible.push(0);
                visible.push(1);
               
                if (info.id == 0) {
                    option1 = info.name;
                    content = "<div class='checkbox'><label><input  id='" + info.id + "' name='layerList' class='listCss' type='checkbox' value='checkbox' onclick='setLayerVisibility()' " + (info.defaultVisibility ? "checked" : "") + " />" + "<img src='./images/3.png'/>" + info.name + "</label></div>";
                }
                else if (info.id == 1) {
                    option2 = info.name;
                    content = "<div class='checkbox'><label><input  id='" + info.id + "' name='layerList' class='listCss' type='checkbox' value='checkbox' onclick='setLayerVisibility()' " + (info.defaultVisibility ? "checked" : "") + " />" + "<img src='./images/2.png'/>" + info.name + "</label></div>";
                }
                else if (info.id == 2) {
                    option3 = info.name;
                    content = "<div class='checkbox'><label><input  id='" + info.id + "' name='layerList' class='listCss' type='checkbox' value='checkbox' onclick='setLayerVisibility()' " + (info.defaultVisibility ? "unchecked" : "") + " />" + "<img src='./images/1.png'/>" + info.name + "</label></div>";
                }
                else if (info.id == 3) {
                    option4 = info.name;
                    content = "<div class='checkbox'><label><input  id='" + info.id + "' name='layerList' class='listCss' type='checkbox' value='checkbox' onclick='setLayerVisibility()' " + (info.defaultVisibility ? "unchecked" : "") + " />" + "<img src='./images/4.png'/>" + info.name + "</label></div>";
                }
                else {
                    continue;
                }
                html = html + content;
                content = "";
            }
            $("#modal-dk .modal-body").append(" <select id='task' class='selectpicker show-tick from-control'><option value='statesTask'>"+option1+"</option><option value='riversTask'>"+option2+"</option><option value='citiesTask'>"+option3+"</option><option value='forthTask'>"+option4+"</option></select>");
            dynamicMapServiceLayer.setVisibleLayers(visible);
            dojo.byId("toc").innerHTML = html;
        }

        setLayerVisibility = function () {
            var inputs = dojo.query(".listCss");
            var showLayer = false;
            visible = [];
            for (var i = 0; i < inputs.length; i++) {
                if (inputs[i].checked) {
                    visible.push(inputs[i].id);
                    showLayer = true;
                }
            }
            if (showLayer) {
                map.addLayer(dynamicMapServiceLayer);
            }
            else {
                map.removeLayer(dynamicMapServiceLayer);
            }
            dynamicMapServiceLayer.setVisibleLayers(visible);
            
        }
    });